package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Invokes the action "registervisitor.jsp"
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = request.getRequestDispatcher("registervisitor.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when you go to register a visitor
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();  
		
		HttpSession session = request.getSession(true);
        
		/**
		 * Parameters obtained form
		 */
	    String n1=request.getParameter("name1");
	    String n2=request.getParameter("name2");
	    String d=request.getParameter("dni");
	    String u=request.getParameter("username");
	    String p1=request.getParameter("password1");
	    String p2=request.getParameter("password2");
	    String e=request.getParameter("email");
	    String t=request.getParameter("phone");
	    String a=request.getParameter("address");
	    
	    VisitorService service = new VisitorService();
	    
	    People people = new People();
	    people.setFirstName(n1);
	    people.setLastName(n2);
	    people.setDni(d);
	    people.setEmail(e);
	    people.setPhoneNumber(t);
	    people.setAddress(a);
	    
	    
	    Visitor visitor = new Visitor();
	    visitor.setUserName(u);
	    visitor.setPassword(p1);
	    
	    try {
	    	/**
	    	 * If the visitor is already registered
	    	 */
			if(service.checkPeople(people)){
				
				request.setAttribute("error", "Known people, please try again");
				
				/**
			     * Invokes the action "login.jsp"
			     */
				response.sendRedirect("login.jsp");
			      
			    /**
			     * If the visitor is not registered
			     */
			}  else {  
			  	
				/**
				 * The visitor is recorded
				 */
				service.createVisitor(people);
				
				visitor.setVisitorId(people.getIdPeople());
				service.createVisitorV(visitor);
				
				session.setAttribute("username", visitor.getUserName());
				session.setAttribute("password", visitor.getPassword());
				
				/**
			     * Invokes the action "login.jsp"
			     */
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		} catch (ServiceException e1) {
			
			e1.printStackTrace();
		} catch (SQLException e1) {
		
			e1.printStackTrace();
		}  
	    
	    out.close(); 
	}

}
